<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="规则名称" prop="ruleName">
      <el-input v-model="dataForm.ruleName" placeholder="规则名称"></el-input>
    </el-form-item>
    <el-form-item label="关键字" prop="keyword">
      <el-input v-model="dataForm.keyword" placeholder="关键字"></el-input>
    </el-form-item>
    <el-form-item label="回复内容" prop="replyContent">
      <el-input   type="textarea"  :rows="5" v-model="dataForm.replyContent" placeholder="回复内容"></el-input>
    </el-form-item>
    <el-form-item label="消息类型" prop="msgType">
      <el-input v-model="dataForm.msgType" placeholder="消息类型"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="validTime">
      <el-date-picker
        v-model="dataForm.validTime"
        type="datetime"
        placeholder="选择过期时间"
        value-format="yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="是否可用" prop="enable">
      <el-radio-group v-model="dataForm.enable">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ruleName: '',
          keyword: '',
          replyContent: '',
          msgType: '',
          validTime: '',
          enable: 1
        },
        dataRule: {
          ruleName: [
            { required: true, message: '规则名称不能为空', trigger: 'blur' }
          ],
          keyword: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          replyContent: [
            { required: true, message: '回复内容不能为空', trigger: 'blur' }
          ],
          msgType: [
            { required: true, message: '消息类型不能为空', trigger: 'blur' }
          ],
          validTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          enable: [
            { required: true, message: '是否可用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cms/msgautoreply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ruleName = data.data.ruleName
                this.dataForm.keyword = data.data.keyword
                this.dataForm.replyContent = data.data.replyContent
                this.dataForm.msgType = data.data.msgType
                this.dataForm.validTime = data.data.validTime
                this.dataForm.enable = data.data.enable
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cms/msgautoreply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'ruleName': this.dataForm.ruleName,
                'keyword': this.dataForm.keyword,
                'replyContent': this.dataForm.replyContent,
                'msgType': this.dataForm.msgType,
                'validTime': this.dataForm.validTime,
                'enable': this.dataForm.enable
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
